package com.yyq.json;

import com.alibaba.fastjson.JSON;
import com.yyq.vo.ignoreInfo;
import org.junit.Test;

import java.io.*;
import java.util.List;

/**
 * @author yyq
 * @create 2021-11-09 18:46
 */
public class readJson {

    public static String PATH=readJson.class.getClassLoader().getResource("ignoreInfo.json").getPath();
    @Test
    public void test(){
        String path = this.getClass().getClassLoader().getResource("ignoreInfo.json").getPath();
        String s = ReadFile(path);
        ignoreInfo test = JSON.parseObject(s, ignoreInfo.class);
        List<ignoreInfo.ignore_path> ignore_paths = test.ignore_paths;

    }

    public static boolean containPath(String path){
        boolean flag=false;
        String s = ReadFile(PATH);
        ignoreInfo info = JSON.parseObject(s, ignoreInfo.class);
        List<ignoreInfo.ignore_path> ignore_paths = info.ignore_paths;
        for (int i = 0; i < ignore_paths.size(); i++) {
            if(path.equals(ignore_paths.get(i).path)){
                flag=true;
                break;
            }
        }
        return flag;
    }

    public static boolean containFile(String path){
        boolean flag=false;
        String s = ReadFile(PATH);
        ignoreInfo info = JSON.parseObject(s, ignoreInfo.class);
        List<ignoreInfo.ignore_file> ignore_files = info.ignore_files;
        for (int i = 0; i < ignore_files.size(); i++) {
            if(path.equals(ignore_files.get(i).filename)){
                flag=true;
                break;
            }
        }
        return flag;
    }

    public static String ReadFile(String Path){
        BufferedReader reader = null;
        String laststr = "";
        try{
            FileInputStream fileInputStream = new FileInputStream(Path);
            InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
            reader = new BufferedReader(inputStreamReader);
            String tempString = null;
            while((tempString = reader.readLine()) != null){
                laststr += tempString;
            }
            reader.close();
        }catch(IOException e){
            e.printStackTrace();
        }finally{
            if(reader != null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return laststr;
    }

}
